package com.it.controller.admin;

import com.it.admin.AdminProductService;
import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.params.guest.AdminGuestQuery;
import com.it.pojo.params.product.AdminProductEditQuery;
import com.it.pojo.params.product.AdminProductQuery;
import com.it.pojo.params.product.AdminProductSaveQuery;
import com.it.pojo.vo.product.AdminProductEditVO;
import com.it.pojo.vo.product.AdminProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/product")
@Api(value = "v1", tags = "商品相关接口")
public class AdminProductController {


    @Resource
    private AdminProductService adminProductService;

    @PostMapping("list")
    @ApiOperation(value = "查询商品信息", notes = "查询商品信息")
    public R<?> list(@RequestBody AdminProductQuery adminProductQuery) {
        if (adminProductQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("adminProductQuery = " + adminProductQuery);
        Map<String, Object> map = adminProductService.listAdminProductVO(adminProductQuery);
        return R.success("成功", map);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增", notes = "新增")
    public R<?> save(
            @RequestBody AdminProductSaveQuery adminProductQuery
                     ) {
        if (adminProductQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        // TODO 参数校验不做了
        System.out.println("adminProductQuery = " + adminProductQuery);
        return adminProductService.insertProduct(adminProductQuery) > 1 ? R.success() : R.error();
    }


    @DeleteMapping("{productId}")
    public R<?> delete(@PathVariable("productId") Integer productId) {
        if (null == productId) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return adminProductService.deleteProductById(productId) > 1 ? R.success() : R.error();
    }

    @PutMapping("")
    public R<?> update(@RequestBody AdminProductEditQuery adminProductEditQuery) {
        if (null == adminProductEditQuery) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return adminProductService.updateProductById(adminProductEditQuery)
                > 0 ? R.success() : R.error();

    }

    @GetMapping("{productId}")
    public R<?> get(@PathVariable("productId") Integer productId) {
        if (null == productId) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        AdminProductEditVO adminProductEditVO = adminProductService.getProduct(productId);

        return R.success("成功", adminProductEditVO);
    }
}
